package com.handydata.antifraud.service.impl;

import com.handydata.antifraud.bo.phonehaoduanbasedata.PhoneHaoduanBasedataBeanBO;
import com.handydata.antifraud.bo.phonehaoduanbasedata.PhoneHaoduanBasedataReqBO;
import com.handydata.antifraud.bo.phonehaoduanbasedata.PhoneHaoduanBasedataResBO;
import com.handydata.antifraud.common.exception.PhoneHaoduanBasedataExCode;
import com.handydata.antifraud.dao.PhoneHaoduanBasedataDao;
import com.handydata.antifraud.entity.PhoneHaoduanBasedataDO;
import com.handydata.antifraud.service.PhoneHaoduanBasedataService;
import com.handydata.common.convert.CommonBeanConvertor;
import com.handydata.common.exception.SysException;
import com.handydata.common.service.AbsService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service("phoneHaoduanBasedataService")
public class PhoneHaoduanBasedataServiceImpl extends AbsService implements PhoneHaoduanBasedataService {
	
	private static final Logger logger = LoggerFactory.getLogger(PhoneHaoduanBasedataServiceImpl.class);
	
	@Autowired
	private PhoneHaoduanBasedataDao phoneHaoduanBasedataDao;

	@Transactional
	@Override
	public PhoneHaoduanBasedataResBO createPhoneHaoduanBasedata(PhoneHaoduanBasedataReqBO req) throws SysException{
	    PhoneHaoduanBasedataResBO res = new PhoneHaoduanBasedataResBO();
		try {
			PhoneHaoduanBasedataDO entity = CommonBeanConvertor.convert(req, PhoneHaoduanBasedataDO.class);
			long status = phoneHaoduanBasedataDao.insert(entity);
			if(status == 0) {
				res.setExCode(PhoneHaoduanBasedataExCode.SYS_NO_ENTITY_CREATED);
			} else {
				res.setExCode(PhoneHaoduanBasedataExCode.SUCCESS);
			}
 			
		} catch (Exception e) {
			logger.error("Create new entity {} get into error", req, e);
			throw new SysException(PhoneHaoduanBasedataExCode.SYS_CREATE_ENTITY_ERROR);
		}
		
		return res;
	}

	@Transactional
	@Override
	public PhoneHaoduanBasedataResBO modifyPhoneHaoduanBasedata(PhoneHaoduanBasedataReqBO req) throws SysException{
	    PhoneHaoduanBasedataResBO res = new PhoneHaoduanBasedataResBO();
		try {
			PhoneHaoduanBasedataDO condition = CommonBeanConvertor.convert(req, PhoneHaoduanBasedataDO.class);
			long status = phoneHaoduanBasedataDao.update(condition);
			if(status == 0) {
				res.setExCode(PhoneHaoduanBasedataExCode.SYS_NO_ENTITY_MODIFIED);
			} else {
				res.setExCode(PhoneHaoduanBasedataExCode.SUCCESS);
			}
		} catch (Exception e) {
			logger.error("Modify entity {} get into error", req, e);
			throw new SysException(PhoneHaoduanBasedataExCode.SYS_MODIFY_ENTITY_ERROR);
		} 
		return res;
	}

	@Transactional
	@Override
	public PhoneHaoduanBasedataResBO deletePhoneHaoduanBasedata(PhoneHaoduanBasedataReqBO req) throws SysException{
	    PhoneHaoduanBasedataResBO res = new PhoneHaoduanBasedataResBO();
		
		try {
			PhoneHaoduanBasedataDO condition = CommonBeanConvertor.convert(req, PhoneHaoduanBasedataDO.class);
			long status = phoneHaoduanBasedataDao.delete(condition);
			if(status == 0) {
				res.setExCode(PhoneHaoduanBasedataExCode.SYS_NO_ENTITY_DELETED);
			} else {
				res.setExCode(PhoneHaoduanBasedataExCode.SUCCESS);
			}
		} catch (Exception e) {
			logger.error("Delete entity {} get into error", req, e);
			throw new SysException(PhoneHaoduanBasedataExCode.SYS_DELETE_ENTITY_ERROR);
		}
		return res;
	}

	@Override
	public PhoneHaoduanBasedataResBO queryPhoneHaoduanBasedata(PhoneHaoduanBasedataReqBO req) throws SysException{
		PhoneHaoduanBasedataDO condition = CommonBeanConvertor.convert(req, PhoneHaoduanBasedataDO.class);
		PhoneHaoduanBasedataResBO res;
		
		try {
			List<PhoneHaoduanBasedataDO> entities = phoneHaoduanBasedataDao.query(condition);
			res = CommonBeanConvertor.convert(entities, PhoneHaoduanBasedataResBO.class, PhoneHaoduanBasedataBeanBO.class);
		} catch(Exception e) {
			logger.error("Query entity {} get into error", req, e);
			throw new SysException(PhoneHaoduanBasedataExCode.SYS_QUERY_ENTITY_ERROR);
		}
		return res;
	}
	
}
